home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / yahoo / peerfiletransfer.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  9KB  |  286 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from common.filetransfer import IncomingFileTransfer
  5. from util.primitives import Storage
  6. from httplib import HTTPConnection
  7. from urllib import quote
  8. from util.net import FileChunker
  9. from path import path
  10. from urlparse import urlparse
  11. from util.threads.threadpool2 import threaded
  12. from common.filetransfer import OutgoingFileTransfer
  13. from random import choice
  14. from traceback import print_exc
  15. from yahoo.yfiletransfer import OutgoingYHTTPXfer
  16. import dns
  17. import logging
  18. log = logging.getLogger('yahoo.peerft')
  19. info = log.info
  20. error = log.error
  21.  
  22. class YahooPeerFileTransfer(IncomingFileTransfer):
  23.     
  24.     def __init__(self, yahoo, buddy, **kws):
  25.         IncomingFileTransfer.__init__(self)
  26.         self.yahoo = yahoo
  27.         self.buddy = buddy
  28.         self.name = kws['filename']
  29.         self.packet = Storage(**kws)
  30.         self.numfiles = 1
  31.         self.size = int(kws['filesize'])
  32.         self.on_get_buddy(self.buddy)
  33.  
  34.     
  35.     def protocol(self):
  36.         return self.yahoo
  37.  
  38.     protocol = property(protocol)
  39.     
  40.     def decline(self):
  41.         self.yahoo.send('peerrequest', 'available', [
  42.             'frombuddy',
  43.             self.packet.to,
  44.             'to',
  45.             self.packet.buddyname,
  46.             'p2pcookie',
  47.             self.packet.p2pcookie,
  48.             'acceptxfer',
  49.             '4'])
  50.         self.state = self.states.CANCELLED_BY_YOU
  51.         IncomingFileTransfer.decline(self)
  52.  
  53.     
  54.     def accept(self, openfile):
  55.         self.packet.acceptxfer = '3'
  56.         self.openfile = openfile
  57.         self.yahoo.send('peerrequest', 'available', [
  58.             'frombuddy',
  59.             self.packet.to,
  60.             'to',
  61.             self.packet.buddyname,
  62.             'p2pcookie',
  63.             self.packet['p2pcookie'],
  64.             'acceptxfer',
  65.             '3'])
  66.         self.state = self.states.CONNECTING
  67.  
  68.     
  69.     def connect(self, buddy, filename, peer_path, p2pcookie, peerip, transfertype):
  70.         myname = self.yahoo.self_buddy.name
  71.         if transfertype == '1':
  72.             u = urlparse(peerip)
  73.             url = u.path + '?' + u.query
  74.             peerip = u.netloc
  75.         else:
  76.             url = '/relay?token=%s&sender=%s&recver=%s' % (quote(peer_path), myname, buddy)
  77.         if peerip:
  78.             info('Got a relay IP; connecting...')
  79.             info('HTTP HEAD: %s', peerip)
  80.             relayconn = HTTPConnection(peerip)
  81.             headers = {
  82.                 'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5)',
  83.                 'Accept': '*/*',
  84.                 'Cache-Control': 'no-cache',
  85.                 'Cookie': self.yahoo.cookie_str }
  86.             relayconn.request('HEAD', url, headers = headers)
  87.             response = relayconn.getresponse()
  88.             status = response.status
  89.             if status != 200:
  90.                 return log.error('ERROR: HEAD request returned a status of %d', status)
  91.             
  92.             print 'resp1 in'
  93.             print response.read()
  94.             print 'resp1 out'
  95.             relayconn.request('GET', url, headers = headers)
  96.             if transfertype != '1':
  97.                 self.yahoo.send('peerinit', 'available', frombuddy = myname, to = buddy, p2pcookie = p2pcookie, filename = filename, transfertype = '3', peer_path = peer_path)
  98.             
  99.             response = relayconn.getresponse()
  100.             status = response.status
  101.             if status != 200:
  102.                 return log.error('ERROR: GET req returned %d status', status)
  103.             
  104.             
  105.             response.info = lambda : dict(response.getheaders())
  106.             headers = response.info()
  107.             
  108.             bytecounter = lambda : 0
  109.             if self.state != self.states.CANCELLED_BY_YOU:
  110.                 self.state = self.states.TRANSFERRING
  111.                 self.filepath = path(self.openfile.name)
  112.                 gen = FileChunker.tofile_gen(response, self.openfile, self.progress, bytecounter = bytecounter)
  113.                 self.chunker = gen.next()
  114.                 
  115.                 try:
  116.                     gen.next()
  117.                 except StopIteration:
  118.                     (None,)
  119.                     (None,)
  120.                 except:
  121.                     (None,)<EXCEPTION MATCH>StopIteration
  122.                 
  123.  
  124.             (None,)
  125.             if self.state != self.states.CANCELLED_BY_YOU:
  126.                 self._ondone()
  127.             
  128.         else:
  129.             log.warning('No peer IP, buddy will connect to me. NOT IMPLEMENTED')
  130.  
  131.     connect = threaded(connect)
  132.     
  133.     def progress(self, bytes_read):
  134.         self._setcompleted(bytes_read)
  135.         self.bytes_read = bytes_read
  136.  
  137.     
  138.     def cancel(self, me = True, state = None):
  139.         if me:
  140.             self.yahoo.send('peerinit', 'cancel', [
  141.                 'frombuddy',
  142.                 self.packet.to,
  143.                 'to',
  144.                 self.packet.buddyname,
  145.                 'p2pcookie',
  146.                 self.packet.p2pcookie,
  147.                 'error',
  148.                 '-1'])
  149.             if state is None:
  150.                 state = self.states.CANCELLED_BY_YOU
  151.             
  152.             self.state = state
  153.         else:
  154.             self.state = self.states.CANCELLED_BY_BUDDY
  155.         self.chunker.cancelled = True
  156.  
  157.     
  158.     def _ondone(self):
  159.         
  160.         try:
  161.             self.yahoo.file_transfers.pop((self.packet.buddyname, self.packet.p2pcookie))
  162.             IncomingFileTransfer._ondone(self)
  163.         except Exception:
  164.             log.warning('_ondone pop failed')
  165.  
  166.  
  167.  
  168.  
  169. class YahooOutgoingPeerFileXfer(OutgoingYHTTPXfer):
  170.     
  171.     def __init__(self, protocol, buddy = None, fileinfo = None):
  172.         OutgoingYHTTPXfer.__init__(self, protocol, buddy = buddy, fileinfo = fileinfo, initiate = False)
  173.         self.yahoo = protocol
  174.  
  175.     
  176.     def send_offer(self):
  177.         log.critical('send_offer')
  178.         import string as string
  179.         letters = string.ascii_letters + string.digits
  180.         cookie = []
  181.         for _i in xrange(22):
  182.             cookie.append(choice(letters))
  183.         
  184.         self.cookie = cookie = ''.join(cookie) + '$$'
  185.         self.yahoo.file_transfers[(self.buddy.name, self.cookie)] = self
  186.         self.yahoo.send('peerrequest', 'available', [
  187.             'frombuddy',
  188.             self.yahoo.self_buddy.name,
  189.             'to',
  190.             self.buddy.name,
  191.             'p2pcookie',
  192.             self.cookie,
  193.             'acceptxfer',
  194.             '1',
  195.             '266',
  196.             '1',
  197.             '302',
  198.             '268',
  199.             '300',
  200.             '268',
  201.             'filename',
  202.             self.name,
  203.             'filesize',
  204.             self.size,
  205.             '301',
  206.             '268',
  207.             '303',
  208.             '268'])
  209.         self.state = self.states.WAITING_FOR_BUDDY
  210.  
  211.     
  212.     def do_setup(self, buddy, p2pcookie, filename = None, peer_path = None, **k):
  213.         log.critical('do_setup')
  214.         self.state = self.states.CONNECTING
  215.         
  216.         def get_ip():
  217.             for ip in dns.resolver.query('relay.msg.yahoo.com', 'A'):
  218.                 return str(ip)
  219.             
  220.             raise Exception, 'no ip for relay transfer'
  221.  
  222.         get_ip = threaded(get_ip)
  223.         
  224.         def send_later(ip):
  225.             self.host = ip
  226.             self.conn_host = ip
  227.             self.yahoo.send('peersetup', 'available', [
  228.                 'frombuddy',
  229.                 self.yahoo.self_buddy.name,
  230.                 'to',
  231.                 self.buddy.name,
  232.                 'p2pcookie',
  233.                 self.cookie,
  234.                 'filename',
  235.                 self.name,
  236.                 'transfertype',
  237.                 '3',
  238.                 'peerip',
  239.                 ip])
  240.  
  241.         get_ip(success = send_later)
  242.  
  243.     
  244.     def go(self, buddy, p2pcookie, filename, transfertype, peer_path, **k):
  245.         log.critical('go')
  246.         peer_path = quote(peer_path)
  247.         self.http_path = '/relay?token=' + peer_path + '&sender=' + self.yahoo.self_buddy.name + '&recver=' + buddy
  248.         self.state = self.states.TRANSFERRING
  249.         
  250.         try:
  251.             filesize = self.filepath.size
  252.             fileobj = file(self.filepath, 'rb')
  253.         except Exception:
  254.             e = None
  255.             print_exc()
  256.             self.state = self.states.CONN_FAIL
  257.             self.on_error()
  258.  
  259.         self._post_file('', self.yahoo.cookie_str, fileobj = fileobj, filesize = filesize)
  260.  
  261.     
  262.     def cancel(self, me = True):
  263.         self.cancelled = True
  264.         if me:
  265.             self.yahoo.send('peersetup', 'cancel', [
  266.                 'frombuddy',
  267.                 self.yahoo.self_buddy.name,
  268.                 'to',
  269.                 self.buddy.name,
  270.                 'p2pcookie',
  271.                 self.cookie,
  272.                 '66',
  273.                 '-1'])
  274.             self.state = self.states.CANCELLED_BY_YOU
  275.         else:
  276.             self.state = self.states.CANCELLED_BY_BUDDY
  277.         
  278.         try:
  279.             self.conn.close()
  280.             del self.conn
  281.         except:
  282.             pass
  283.  
  284.  
  285.  
  286.